<?php

/**
 * Section Model
 * @author phuongtn
 */
class section
{
    /**
     * @var system_model Singleton instance
     */
    protected static $_instance;

    /**
     * Avoid directly create instance
     */
    private function __construct()
    {

    }

    /**
     * Retrieve singleton instance
     *
     * @return system_model
     */
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Reset the singleton instance
     *
     * @return void
     */
    public static function resetInstance()
    {
        self::$_instance = null;
    }

    /**
     * Add section
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function addSection($params)
    {
        try
        {
            //init model metadata
            $sectionMeta = new Fpt_Business_Block_section_metadata_section();
            //init data
            $sectionMeta->init($params);
            //get formatted data
            $formatParams = $sectionMeta->getData();
            
            //unsert old
            unset($params);
            
            //read config 
            $config = Fpt_Global::getConfig('application');
            //init model db
            $modelSql = Fpt_Business_Block_section::factory($config['block']['db']);
            //add db section
          
            $insertRS = $modelSql->addSection($formatParams);
            $insertRS = true;
            //if insert db success;
            if($insertRS !== false)
            {
                //init model redis
                $sectionRedis = Fpt_Business_Block_section_adapter_redis::getInstance();
                $cacheFormat = $sectionMeta->getDataCache();
                
                $redis =  $sectionRedis->addSection($cacheFormat);
              
                if($redis != false)
                {
                    return $cacheFormat;
                }
            }
            return false;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    public function getSection($sectionName, $status = 1)
    {
        try
        {
            //init model redis
            $sectionRedis = Fpt_Business_Block_section_adapter_redis::getInstance();
            //init params
            $getParams = array('SectionName' => $sectionName, 'SectionStatus' => $status);
            //get redis detail
            $sectionDetail =  $sectionRedis->getSection($getParams);
            
            if(empty($sectionDetail))
            {
                //read config 
                $config = Fpt_Global::getConfig('application');
                //init model db
                $modelSql = Fpt_Business_Block_section::factory($config['block']['db']);
                //add db section
                $sectionDetail = $modelSql->getSection($getParams);
                //if insert db success;
                if($sectionDetail !== false)
                {
                    
                    $sectionRedis->addSection($sectionDetail); 
                    
                }
            }
            return $sectionDetail;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
   /**
     * Get list section
     * @return type
     * @throws Core_Exception 
     */
    public function getListSection()
    {
        try
        {
            $response = false;
            //init model redis
            $sectionRedis = Fpt_Business_Block_section_adapter_redis::getInstance();
          
            //get redis detail
            $sectionList =  $sectionRedis->getListSection();
            
            if(empty($sectionList))
            {
                //read config 
                $config = Fpt_Global::getConfig('application');
                //init model db
                $modelSql = Fpt_Business_Block_section::factory($config['block']['db']);
                //add db section
                $sectionDB = $modelSql->getListSection($getParams);
                
                foreach($sectionDB as $key => $detail)
                {
                    $sectionList[] = $detail['SectionName'];
                }
                $modelLayout = layout::getInstance();
                $modelLayout->backupMap();
            }
           
            //check get list sucess
            if(is_array($sectionList))
            {
                foreach($sectionList as $sectionName)
                {
                    $response[] = $this->getSection($sectionName);
                }
                return $response;
            }
           
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $response;
    }
    
    
}